<template>
  <div class="detail-page" v-if="article.id">
    <van-nav-bar left-text="返回" @click-left="$router.back()" fixed title="面经详情" />
    <header class="header">
      <h1>{{ article.stem }}</h1>
      <p>{{ article.createdAt }} | {{ article.views }} 浏览量 | {{ article.likeCount }} 点赞数</p>
      <p>
        <img :src="article.avatar" alt="" />
        <span>作者: {{ article.creator }}</span>
      </p>
    </header>
    <main class="body" v-html="article.content"></main>
    <div class="opt">
      <van-icon :class="{ active: article.likeFlag === 1 }" name="like-o" @click="updateLike" />
      <van-icon @click="updateCollect" :class="{ active: article.collectFlag === 1 }" name="star-o" />
    </div>
  </div>
  <div v-else>
    <div style="margin-top: 60px;"></div>
    <van-skeleton title avatar :row="3" />
    <div style="margin-top: 30px;"></div>
    <van-skeleton title :row="3" />
    <div style="margin-top: 30px;"></div>
    <van-skeleton title :row="3" />
    <div style="margin-top: 30px;"></div>
    <van-skeleton title :row="3" />
  </div>
</template>

<script>
import { getArticleDetailApi, updateLikeApi, updateCollectApi } from '@/api/detail'
export default {
  name: 'detail-page',
  data () {
    return {
      article: {}
    }
  },
  async created () {
    this.getArticleDetail(this.$route.params.id)
  },
  methods: {
    async  getArticleDetail (id) {
      try {
        const res = await getArticleDetailApi(id)
        this.article = res.data
      } catch (error) {

      }
    },
    async updateLike () {
      await updateLikeApi(this.article.id)
      this.article.likeFlag = this.article.likeFlag === 0 ? 1 : 0
      if (this.article.likeFlag === 1) {
        this.$toast.success('点赞成功')
        this.article.likeCount = Number(this.article.likeCount) + 1
      } else {
        this.$toast.success('取消点赞')
        this.article.likeCount = Number(this.article.likeCount) - 1
      }
    },
    async updateCollect () {
      await updateCollectApi(this.article.id)
      this.article.collectFlag = this.article.collectFlag === 0 ? 1 : 0
      if (this.article.collectFlag === 1) {
        this.$toast.success('收藏成功')
      } else {
        this.$toast.success('取消收藏')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;

  .header {
    h1 {
      font-size: 24px;
    }

    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }

    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }

  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;

    >.van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;

      &.active {
        background: #FEC635;
        color: #fff;
      }
    }
  }
}
</style>
